package satir.command;

import java.util.ArrayList;
import java.util.Scanner;

import satir.SatirGame;
import satir.framework.InputValidator;
import satir.player.Player;

/**
 * Enter Player number.
 * @version 3.0 April 2012
 * @author Creative Family
 */

public class EnterPlayerNumCmd implements Command {

    /** The satir game. */
    private SatirGame satirGame;

    /**
     * Instantiates a new enter player number command.
     * @param satirGame
     *            the Satir game
     */
    public EnterPlayerNumCmd(final SatirGame satirGame) {
	this.satirGame = satirGame;
    }

    /**
     * Execute to retrieve user input.
     */
    public void execute() {

	int playerNum = getPlayerNumber();

	satirGame.setNumOfPlayer(playerNum);
	System.out.println("There are " + playerNum + " players");
	ArrayList<Player> players = new ArrayList<Player>();
	for (int i = 0; i < playerNum; i++) {
	    players.add(new Player());
	}
	satirGame.setArrayListPlayer(players);
    }

    /**
     * Gets the player number.
     *
     * @return the player number
     */
    private int getPlayerNumber() {
	Scanner scanner = new Scanner(System.in);
	String input;
	boolean isInValid = false;
	int playerNum = 0;
	do {
	    System.out.println("Input the number of players:(2-6)");
	    input = scanner.next();
	    isInValid = (!InputValidator.isInteger(input));
	    if (isInValid) {
		System.out.println("Wrong input");
	    } else {
		playerNum = Integer.parseInt(input);
		isInValid = (!InputValidator.isWithinRange(playerNum, 2, 6));
		if (isInValid) {
		    System.out.println("Out of Range!");
		}
	    }
	} while (isInValid);
	return playerNum;
    }
}
